//
//  OosList.m
//  Oos4ios
//
//  Created by Pablo Gonzalez on 3/24/11.
//  Copyright 2011 11870.com. All rights reserved.
//

#import <oos4ios/Model/OosList.h>

@implementation OosList

@dynamic label;
@dynamic term;

@dynamic users;
@dynamic reviews;

static NSMutableDictionary /* NSString, OosList */ *listCache;

static NSString *fetchPredicate = @"term = %@";

+ (void)initialize 
{
    listCache = [[NSMutableDictionary dictionary] retain];
}

+ (OosList *) listFromTerm:(NSString *)term
{
    
    OosList *list = nil;
    list = [listCache objectForKey:term];
    
    if (list == nil) {
        list = [super objectWithPredicate: fetchPredicate, term];
        if (!list) {
            list = [super create];
            list.term = term;
            list.label = term;
        }
    }
    if (list != nil) {
        @synchronized(listCache) {
            [listCache setObject:list forKey:term];
        }
    }
    
    return list;
}

- (NSMutableSet *)mutableUsers
{
    return [self mutableSetValueForKey:@"users"];
}

- (NSMutableSet *)mutableReviews
{
    return [self mutableSetValueForKey:@"reviews"];
}

- (NSString *) description
{
    return self.term;
}


@end
